﻿<%@ Page Title="" Language="C#" MasterPageFile="~/OneColumn.Master" AutoEventWireup="true"
    CodeBehind="InputFinishedProduct.aspx.cs" Inherits="KCL.Input.InputFinishedProduct" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">Nhập thành phẩm
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageHeader" runat="server">
    <link type="text/css" href="../styles/themes/base/jquery.ui.all.css" rel="stylesheet" />
    <script type="text/javascript" src="../styles/ui/jquery.ui.core.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.widget.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.position.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.mouse.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.draggable.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.dialog.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.button.js"></script>
    <script type="text/javascript" src="../styles/external/jquery.bgiframe-2.1.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnSave").button();
        });

        function SubmitUpdate() {
            if ($(".orderId").attr("value") == "0") {
                alert("Hãy chọn đơn hàng để nhập thành phẩm");
                return false;
            }
            var phaseCount = $("input.materialFinishedQuantity").size();
            var orderMaterialIdListClient = "";
            var orderMaterialFinishedQuantityClient = "";

            var selector = "input.materialFinishedQuantity:eq(";
            var delimiter = "#";

            for (var i = 0; i < phaseCount; i++) {
                var orderMaterialId = $(selector + i + ")").attr("id");
                var orderMaterialFinishedQuantity = $("#" + orderMaterialId).attr("Value");
                var isValid = CheckQuantity(orderMaterialId, orderMaterialFinishedQuantity);

                if (isValid == 0) {
                    break;
                }

                orderMaterialIdListClient = orderMaterialIdListClient + orderMaterialId + delimiter;
                orderMaterialFinishedQuantityClient = orderMaterialFinishedQuantityClient + orderMaterialFinishedQuantity + delimiter;
            }

            $.ajax({
                url: "InputFinishedProduct.ashx?",
                type: "POST",
                dataType: "script",
                data: {
                    orderMaterialIdList: orderMaterialIdListClient,
                    orderMaterialFinishedQuantity: orderMaterialFinishedQuantityClient,
                    orderId: $(".orderId").attr("value")
                }
            });
        }

        function CheckQuantity(itemId, newValue) {
            $("#" + itemId).attr("Value", newValue);
            var orderQuantity = $("#orderQuantity" + itemId).html();

            if (parseInt(orderQuantity) < parseInt(newValue)) {
                $("#" + itemId).css("background", "#fffd41");
                alert("Số lượng thành phẩm không được vượt quá số lượng đặt!");
                return 0;
            }
            $("#" + itemId).css("background", "transparent");

            //            $("#" + itemId).attr("Value", parseInt(newValue) + (parseInt(total) - parseInt($(".orderQuantity").attr("Value"))));
        }
    </script>
    <style type="text/css">
        #generalInfo td:first-child
        {
            font-weight: bold;
            padding: 5px 5px 5px 0;
        }
        #generalInfo td:first-child + td input
        {
            border: 0;
            background: transparent;
            color: #000000;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <table>
        <tr>
            <td colspan="5">
                <span style="font-size: 25px; color: #474747">NHẬP THÀNH PHẨM</span>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td>
                <table id="generalInfo">
                    <tr>
                        <td>
                            <span>Mã đơn hàng:</span>&nbsp;&nbsp;
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlOrderID" runat="server" CssClass="orderId" Width="120" 
                                AutoPostBack="true" onselectedindexchanged="ddlOrderID_SelectedIndexChanged">
                            </asp:DropDownList>                            
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span>Khách hàng:</span>&nbsp;&nbsp;
                        </td>
                        <td>
                            <%=txtCustomerId %>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span>Số đơn hàng:</span>&nbsp;&nbsp;
                        </td>
                        <td>
                            <%=txtOrderCode %>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span>Ngày bắt đầu:</span>&nbsp;&nbsp;
                        </td>
                        <td>
                            <%=txtCreatedDate %>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span>Ngày kết thúc:</span>&nbsp;&nbsp;
                        </td>
                        <td>
                            <%=txtDeliveryDate %>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <asp:ObjectDataSource ID="dsUnFinishedProducts" TypeName="KCL.Code.Data.Search" SortParameterName="sortExpression"
        SelectMethod="GetInput" SelectCountMethod="GetTotalInput" EnablePaging="True"
        runat="server">
        <SelectParameters>
            <asp:QueryStringParameter Name="orderId" QueryStringField="orderId" Type="Int64" />
            <asp:Parameter Name="sortExpression" Type="String" />
            <asp:Parameter Name="maximumRows" Type="Int32" />
            <asp:Parameter Name="startRowIndex" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
    <asp:GridView ID="gridUnFinishedProduct" AllowSorting="True" AutoGenerateColumns="False"
        BackColor="Wheat" BorderColor="Firebrick" BorderStyle="Ridge" BorderWidth="1px"
        CellPadding="7" DataSourceID="dsUnFinishedProducts" GridLines="None" Width="100%"
        runat="server" EnableModelValidation="True">
        <HeaderStyle BackColor="Black" ForeColor="Azure" Font-Bold="true" Wrap="false" HorizontalAlign="Left" />
        <FooterStyle ForeColor="Maroon" BackColor="DarkGoldenrod" />
        <PagerStyle ForeColor="Snow" HorizontalAlign="Right" BackColor="SaddleBrown" />
        <RowStyle BackColor="Ivory" />
        <Columns>
            <asp:BoundField HeaderText="Mã nguyên liệu" DataField="materialId" SortExpression="materialId" />
            <asp:BoundField HeaderText="Quy cách" DataField="description" SortExpression="description" />
            <asp:BoundField HeaderText="Công đoạn" DataField="name" SortExpression="name" />
            <%--<asp:BoundField HeaderText="Số lượng đặt" DataField="orderQuantity" SortExpression="orderQuantity"/>--%>
            <asp:TemplateField HeaderText="Số lượng đặt">
                <ItemTemplate>
                    <span id="orderQuantity<%# Eval("materialId") %>">
                        <%# Eval("orderQuantity")%></span>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Số lượng hoàn thành">
                <ItemTemplate>
                    <input id="<%# Eval("materialId") %>" class="materialFinishedQuantity" type="text"
                        value="<%# Eval("finishedQuantity") %>" onchange="CheckQuantity(this.id, this.value)" style="width: 35px;"/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <AlternatingRowStyle BackColor="SeaShell" />
    </asp:GridView>
    <br />
    <input type="button" id="btnSave" value="&nbsp;&nbsp;Lưu&nbsp;&nbsp;" onclick="SubmitUpdate()" />
</asp:Content>